using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Resource.@string;
using MicroMsg.Scene;
using MicroMsg.Scene.Voice;
using MicroMsg.Storage;
using MicroMsg.UI.Util;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
namespace MicroMsg.UI.UserContrl
{
	public class ShowBottleMsgCtrl : UserControl
	{
		private const string TAG = "ShowBottleMsgCtrl";
		private UITaskDlgExHasRet mHandler;
		private ChatMsg mBtlMsg;
		private bool mBIsFirstLoaded = true;
		private EventWatcher mWatcherHeadImgAdd;
		private EventWatcher mWatcherExpose;
		private MsgTrans mMsgTrans;
		private BottleContact mContact;
		private VoiceService mVoiceService;
		internal Grid LayoutRoot;
		internal MediaElement MMAudioPlayer;
		internal StackPanel panelTop;
		internal Button btnSeeBigHead;
		internal Image imgHead;
		internal StackPanel panelSexAndArea;
		internal Image imgSex;
		internal ScrollTextBlock txtArea;
		internal ScrollTextBlock txtSignature;
		internal Canvas canvasLetterCon;
		internal ScrollViewer svTextHolder;
		internal TextBlock txtBottleContent;
		internal StackPanel panelLines;
		internal Grid canvasVoiceCon;
		internal Button btnThrowBack;
		internal Button btnReply;
		private bool _contentLoaded;
		public ShowBottleMsgCtrl()
		{
			this.Construct();
		}
		public ShowBottleMsgCtrl(UITaskDlgExHasRet handler)
		{
			this.Construct();
			this.mHandler = handler;
		}
		private void Construct()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.Control_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.Control_Unloaded));
			this.LayoutRoot.set_Visibility(1);
		}
		private void Control_Loaded(object sender, RoutedEventArgs e)
		{
			if (this.mVoiceService != null)
			{
				this.mVoiceService.Init();
			}
			Log.smethod_5("ShowBottleMsgCtrl", "Control_Loaded");
			if (this.mBIsFirstLoaded)
			{
				this.mBIsFirstLoaded = false;
				this.initUI();
				this.LayoutRoot.set_Visibility(0);
				this.doWithFather(ShowBtleRet.RetKind_CtrlLoaded);
			}
			this.registerEvent();
		}
		private void Control_Unloaded(object sender, RoutedEventArgs e)
		{
			if (this.mVoiceService != null)
			{
				this.mVoiceService.UnInit();
			}
			Log.smethod_5("ShowBottleMsgCtrl", "Control_Unloaded");
			this.unRegisterEvent();
		}
		private void registerEvent()
		{
			if (this.mWatcherHeadImgAdd == null)
			{
				this.mWatcherHeadImgAdd = new EventWatcher(this, null, new EventHandlerDelegate(this.onEventHandlerHeadImgAdd));
				EventCenter.registerEventWatcher(EventConst.ON_MGR_HEAD_IMG_READY, this.mWatcherHeadImgAdd);
			}
			if (this.mWatcherExpose == null)
			{
				this.mWatcherExpose = new EventWatcher(this, null, new EventHandlerDelegate(this.onEventHandlerExpose));
				EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_EXPOSE_SUCCESS, this.mWatcherExpose);
				EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_EXPOSE_ERR, this.mWatcherExpose);
			}
		}
		private void unRegisterEvent()
		{
			if (this.mWatcherHeadImgAdd != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_MGR_HEAD_IMG_READY, this.mWatcherHeadImgAdd);
				this.mWatcherHeadImgAdd = null;
			}
			if (this.mWatcherExpose != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_EXPOSE_SUCCESS, this.mWatcherExpose);
				EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_EXPOSE_ERR, this.mWatcherExpose);
				this.mWatcherExpose = null;
			}
		}
		private void onEventHandlerHeadImgAdd(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (this.mContact == null || string.IsNullOrEmpty(this.mContact.strUserName))
			{
				return;
			}
			List<string> listObject = evtArgs.getListObject<string>();
			if (listObject != null && this.mBtlMsg != null && this.mBtlMsg.strTalker != null)
			{
				using (List<string>.Enumerator enumerator = listObject.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						string current = enumerator.get_Current();
						if (current != null && current.GetType() == typeof(string) && current == this.mContact.strUserName)
						{
							this.imgHead.set_Source(HeadImgMgr.getHeadImage(this.mContact));
						}
					}
				}
				return;
			}
		}
		private void onEventHandlerExpose(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
			if (EventConst.ON_NETSCENE_EXPOSE_SUCCESS == evtArgs.mEventID)
			{
				MessageBoxManager.ShowToast(strings.Lbs_PeoInfo_MsgExposeSucc, 2500, null, false);
				return;
			}
			if (EventConst.ON_NETSCENE_EXPOSE_ERR == evtArgs.mEventID)
			{
				MMMessageBox.Show(strings.Lbs_PeoInfo_MsgExposeFail, true, "确定");
			}
		}
		private void btnSeeBigHead_Click(object sender, RoutedEventArgs e)
		{
			if (this.mContact != null && this.mContact.strUserName != null)
			{
				ToolFunction.GotoPageForUserControl("/Source/UI/Contact/HighQualityHeadImgView.xaml", new object[]
				{
					this.imgHead.get_Source(),
					this.mContact.strUserName,
					new Point(38.0, 57.0)
				}, false);
				return;
			}
		}
		private void btnThrowBack_Click(object sender, RoutedEventArgs e)
		{
			this.doWithFather(ShowBtleRet.RetKind_Throw);
		}
		private void btnReply_Click(object sender, RoutedEventArgs e)
		{
			this.doWithFather(ShowBtleRet.RetKind_Reply);
		}
		private void btnExpose_Click(object sender, RoutedEventArgs e)
		{
			MMMessageBox mMMessageBox = MMMessageBox.Show(strings.Lbs_PeoInfo_MsgExposeTitle, strings.Lbs_PeoInfo_MsgExposeContent, true, strings.COM_Yes, strings.COM_No);
			mMMessageBox.ReturnEvent = new DlgReturnEvent(this.msgBoxExposeHandler);
			this.doWithFather(ShowBtleRet.RetKind_BackKeyDisable);
		}
		private void msgBoxExposeHandler(DlgReturnEventArg dlgReturnEventArg_0)
		{
			this.doWithFather(ShowBtleRet.RetKind_BackKeyEnable);
			if (MMMessageBox.GetUserClickResult(dlgReturnEventArg_0) == 0)
			{
				ServiceCenter.sceneExpose.doScene(this.mContact.strUserName, ExposeScene.MM_EXPOSE_BOTTLE_PICK, 0u);
			}
		}
		private object doWithFather(ShowBtleRet showBtleRet)
		{
			if (this.mHandler != null)
			{
				return this.mHandler(showBtleRet);
			}
			return null;
		}
		private void initUI()
		{
			object obj = this.doWithFather(ShowBtleRet.RetKind_GetBtle);
			if (obj != null && obj is ChatMsg)
			{
				this.mBtlMsg = (obj as ChatMsg);
				int nMsgType = this.mBtlMsg.nMsgType;
				switch (nMsgType)
				{
				case 1:
					this.setTxtBtlMode();
					goto IL_6C;
				case 2:
					break;
				case 3:
					this.doWithFather(ShowBtleRet.RetKind_JustBack);
					return;
				default:
					if (nMsgType == 34)
					{
						this.setVoiceBtlMode();
						goto IL_6C;
					}
					if (nMsgType == 43)
					{
						this.doWithFather(ShowBtleRet.RetKind_JustBack);
						return;
					}
					break;
				}
				this.doWithFather(ShowBtleRet.RetKind_JustBack);
				IL_6C:
				this.disSenderInfo();
				return;
			}
			this.doWithFather(ShowBtleRet.RetKind_JustBack);
		}
		private void drawLines(double nLineHeight, double nLineWidth, double nContentHeight, double nMinHeight)
		{
			BitmapImage source = new BitmapImage(new Uri("/Source/Plugin/DriftBottle/Resource/Img/LetterLine.png", 2));
			Thickness margin = new Thickness(0.0, nLineHeight, 0.0, 0.0);
			if (nMinHeight > nContentHeight)
			{
				nContentHeight = nMinHeight;
			}
			int num = (int)(nContentHeight / nLineHeight);
			for (int i = 0; i < num; i++)
			{
				Image image = new Image();
				image.set_Margin(margin);
				image.set_Width(nLineWidth);
				image.set_Height(1.0);
				image.set_Stretch(1);
				image.set_Source(source);
				this.panelLines.get_Children().Add(image);
			}
			this.panelLines.UpdateLayout();
		}
		private void setTxtBtlMode()
		{
			if (this.mBtlMsg == null)
			{
				return;
			}
			this.mBtlMsg.nStatus = 1;
			StorageMgr.chatMsg.modifyMsg(this.mBtlMsg);
			BConversationMgr.setUnreadCount(this.mBtlMsg.strTalker, 0, true);
			this.canvasLetterCon.set_Visibility(0);
			this.canvasVoiceCon.set_Visibility(1);
			this.txtBottleContent.set_Text(this.mBtlMsg.strMsg);
			this.drawLines(45.0, 375.0, this.txtBottleContent.get_ActualHeight(), this.panelLines.get_MinHeight());
		}
		private void setVoiceBtlMode()
		{
			if (this.mBtlMsg == null)
			{
				return;
			}
			BConversationMgr.setUnreadCount(this.mBtlMsg.strTalker, 0, false);
			this.canvasLetterCon.set_Visibility(1);
			this.canvasVoiceCon.set_Visibility(0);
			DriftBottleVoiceItem driftBottleVoiceItem = new DriftBottleVoiceItem();
			driftBottleVoiceItem.SetChatMsg(this.mBtlMsg);
			string text = "";
			string xmlStr = "";
			if (!ChatMsgMgr.getMsgUser(this.mBtlMsg, ref text, ref xmlStr))
			{
				Log.smethod_1("ShowBottleMsgCtrl", "getMsgUser failed,content is: " + this.mBtlMsg.strMsg);
				return;
			}
			this.mMsgTrans = new MsgTrans();
			if (DownloadVoiceService.parseVoiceMsgXML(xmlStr, this.mMsgTrans))
			{
				driftBottleVoiceItem.SetVoiceLen((this.mMsgTrans.nDuration + 499) / 1000);
			}
			else
			{
				driftBottleVoiceItem.SetVoiceLen(1);
				Log.smethod_1("ShowBottleMsgCtrl", "DownloadVoiceService.parseVoiceMsgXML error:nMsgLocalID(" + this.mBtlMsg.nMsgLocalID + ")");
			}
			driftBottleVoiceItem.SetItemClick(new ChatItemClick(this.VoiceItemClickHandle));
			this.canvasVoiceCon.get_Children().Add(driftBottleVoiceItem);
		}
		private void disSenderInfo()
		{
			if (this.mBtlMsg == null)
			{
				return;
			}
			string gparam_ = BContactMgr.toBottleContactUserName(this.mBtlMsg.strTalker);
			BottleContact bottleContact = StorageMgr.bContact.method_0(gparam_);
			if (bottleContact == null)
			{
				return;
			}
			this.mContact = bottleContact;
			this.txtArea.Text = strings.Float_Bottle_City_Format + ToolFunction.GetRegionShow(bottleContact.strProvince, bottleContact.strCity);
			this.txtSignature.Text = bottleContact.strSignature;
			if (1 == bottleContact.nSex)
			{
				this.imgSex.set_Source(new BitmapImage(new Uri("/Resource/Img/CommonUse/SexIco_man.png", 2)));
			}
			else
			{
				if (2 == bottleContact.nSex)
				{
					this.imgSex.set_Source(new BitmapImage(new Uri("/Resource/Img/CommonUse/SexIco_woman.png", 2)));
				}
				else
				{
					this.imgSex.set_Visibility(1);
				}
			}
			this.imgHead.set_Source(HeadImgMgr.getHeadImage(this.mContact));
		}
		private void VoiceItemClickHandle(ChatMsg chatMsg_0, BaseChatElement chatElement)
		{
			if (chatMsg_0 == null)
			{
				return;
			}
			if (!chatMsg_0.IsVoice())
			{
				return;
			}
			if (this.mMsgTrans == null)
			{
				return;
			}
			ChatMsgMgr.setVoiceMsgIsRead(chatMsg_0, true);
			BConversationMgr.setUnreadCount(chatMsg_0.strTalker, 0, true);
			DriftBottleVoiceItem driftBottleVoiceItem = chatElement as DriftBottleVoiceItem;
			if (driftBottleVoiceItem == null)
			{
				return;
			}
			string downVoiceFileNameForPlugin = MsgVoiceStorage.getDownVoiceFileNameForPlugin("DriftBottle", chatMsg_0.nMsgSvrID);
			if (this.mVoiceService == null)
			{
				this.mVoiceService = new VoiceService(this.MMAudioPlayer);
				if (this.mVoiceService == null)
				{
					return;
				}
			}
			this.mVoiceService.Init();
			this.mVoiceService.ClickVoice(downVoiceFileNameForPlugin, chatMsg_0, driftBottleVoiceItem);
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/DriftBottle/UI/ShowBottleMsgCtrl.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.MMAudioPlayer = (MediaElement)base.FindName("MMAudioPlayer");
			this.panelTop = (StackPanel)base.FindName("panelTop");
			this.btnSeeBigHead = (Button)base.FindName("btnSeeBigHead");
			this.imgHead = (Image)base.FindName("imgHead");
			this.panelSexAndArea = (StackPanel)base.FindName("panelSexAndArea");
			this.imgSex = (Image)base.FindName("imgSex");
			this.txtArea = (ScrollTextBlock)base.FindName("txtArea");
			this.txtSignature = (ScrollTextBlock)base.FindName("txtSignature");
			this.canvasLetterCon = (Canvas)base.FindName("canvasLetterCon");
			this.svTextHolder = (ScrollViewer)base.FindName("svTextHolder");
			this.txtBottleContent = (TextBlock)base.FindName("txtBottleContent");
			this.panelLines = (StackPanel)base.FindName("panelLines");
			this.canvasVoiceCon = (Grid)base.FindName("canvasVoiceCon");
			this.btnThrowBack = (Button)base.FindName("btnThrowBack");
			this.btnReply = (Button)base.FindName("btnReply");
		}
	}
}
